package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.LegalTender;
import com.ruoyi.web.service.ILegalTenderService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 法币Controller
 *
 * @date 2023-03-15
 */
@RestController
@RequestMapping("/legal-tender")
@AllArgsConstructor
public class LegalTenderController extends BaseController {

    private ILegalTenderService legalTenderService;

    /**
     * 查询法币列表
     */
    @PreAuthorize("@ss.hasPermi('web:legalTender:list')")
    @GetMapping("/list")
    public TableDataInfo list(LegalTender legalTender) {
        startPage();
        List<LegalTender> list = legalTenderService.selectLegalTenderList(legalTender);
        return getDataTable(list);
    }

    /**
     * 导出法币列表
     */
    @PreAuthorize("@ss.hasPermi('web:legalTender:export')")
    @Log(title = "法币", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LegalTender legalTender) {
        List<LegalTender> list = legalTenderService.selectLegalTenderList(legalTender);
        ExcelUtil<LegalTender> util = new ExcelUtil<LegalTender>(LegalTender.class);
        util.exportExcel(response, list, "法币数据");
    }

    /**
     * 获取法币详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:legalTender:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(legalTenderService.selectLegalTenderById(id));
    }

    /**
     * 新增法币
     */
    @PreAuthorize("@ss.hasPermi('web:legalTender:add')")
    @Log(title = "法币", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LegalTender legalTender) {
        return toAjax(legalTenderService.insertLegalTender(legalTender));
    }

    /**
     * 修改法币
     */
    @PreAuthorize("@ss.hasPermi('web:legalTender:edit')")
    @Log(title = "法币", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LegalTender legalTender) {
        return toAjax(legalTenderService.updateLegalTender(legalTender));
    }

    /**
     * 删除法币
     */
    @PreAuthorize("@ss.hasPermi('web:legalTender:remove')")
    @Log(title = "法币", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(legalTenderService.deleteLegalTenderByIds(ids));
    }

    /**
     * 选项
     *
     * @return {@link TableDataInfo}
     * @since 2023/3/16 21:35
     */
    @GetMapping("/option")
    public TableDataInfo option() {
        List<LegalTender> list = legalTenderService.optionList();
        return getDataTable(list);
    }

    /**
     * h5页面查询所有法币列表
     *
     * @return {@link TableDataInfo}
     * @since 2023/3/16 21:35
     */
    @GetMapping("/app/list")
    public TableDataInfo appList() {
        List<LegalTender> list = legalTenderService.optionList();
        return getDataTable(list);
    }
}
